package com.crr.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.crr.demo.bean.User;
import com.crr.demo.common.ResultModel;
import com.crr.demo.impl.service.UserService;
import com.crr.demo.utils.DateUtils;
import com.crr.demo.utils.JWTUtils;


import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
public class LoginController {
    @Resource
    UserService userService;

    /**
     * 网页登录
     * @param user
     * @return
     */
    @PostMapping("/web/login")
    public ResultModel login(@RequestBody User user) {
        if(user.getIdentityId()==null){
            return new ResultModel(103,DateUtils.getTime());
        }
        user=userService.getUserByLogin(user.getIdentityId(),user.getPwd());
        if(user.getActive().longValue()==0){//判断是否激活
            return new ResultModel(102,DateUtils.getTime());
        }
        if(user!=null){//判断用户名与密码是否存在数据库
            //登陆成功，产生token令牌
            String token=JWTUtils.sign(user.getIdentityId());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("token",token);
            return new ResultModel(jsonObject,100,DateUtils.getTime());
        }
        return new ResultModel(104,DateUtils.getTime());
    }

}
